<%@ page import="org.apache.poi.ss.usermodel.*" %>
<%@ page contentType="application/vnd.ms-excel;charset=UTF-8" language="java" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook" %>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFRichTextString" %>
<%@ page import="dao.TuitionRecordDao" %>
<%@ page import="vo.TuitionRecord" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<!DOCTYPE html>
    <html>
    <head>
        <title>main</title>
    </head>
    <body>
        <%
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=excel.xls"+";filename*=utf-8''excel.xls");
            Workbook workbook = new HSSFWorkbook();
            Sheet sheet = workbook.createSheet("Sheet1");
            Row titleRow = sheet.createRow(0);
            titleRow.createCell(0).setCellValue(new HSSFRichTextString("学号"));
            titleRow.createCell(1).setCellValue(new HSSFRichTextString("姓名"));
            titleRow.createCell(2).setCellValue(new HSSFRichTextString("缴费金额"));
            titleRow.createCell(3).setCellValue(new HSSFRichTextString("缴费时间"));
            try {
                TuitionRecordDao tuitionRecordDao = new TuitionRecordDao();
                List<TuitionRecord> tuitionRecords = tuitionRecordDao.findAll();
                int rowNum = 1;
                for(TuitionRecord t : tuitionRecords){
                    Row row = sheet.createRow(rowNum++);
                    row.createCell(0).setCellValue(t.getStudentId());
                    row.createCell(1).setCellValue(t.getStudentName());
                    row.createCell(2).setCellValue(t.getAmount());
                    row.createCell(3).setCellValue(t.getPaymentDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            OutputStream output = response.getOutputStream();
            workbook.write(output);
            output.close();
        %>
    </body>
</html>